package core;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by xe on 15-6-4.
 */
public final class ResponseUtil {

    private static final String HTTP_ENCODE = "UTF-8";

    private static final String RESP_PREFIX_MARKER = "**YGKJ";
    private static final String RESP_SUFFIX_MARKER = "YGKJ##";
    private static int RESP_PREFIX_MARKER_LENGTH = RESP_PREFIX_MARKER.length();
    private static final String RESP_PREFIX_MARKER_WITH_JSONR = RESP_PREFIX_MARKER + "{\"jsonr\":";
    private static final String RESP_SUFFIX_MARKER_WITH_JSONR = "}" + RESP_SUFFIX_MARKER;
    private static int RESP_PREFIX_MARKER_WITH_JSONR_LENGTH = RESP_PREFIX_MARKER_WITH_JSONR.length();

    /**
     * status是否有效
     */
    public static boolean isStatusAvailable(String status) {
        return "00".equals(status);
    }

    /**
     * 提取有效的 json 内容,保留jsonr
     */
    public static String pruneRespString(String rawRespString) {
        if (null == rawRespString) {
            return null;
        }
        int prefixMarkerPosition;
        int suffixMarkerPosition;
        if (null != rawRespString
                && -1 != (prefixMarkerPosition = rawRespString.indexOf(RESP_PREFIX_MARKER))
                && -1 != (suffixMarkerPosition = rawRespString.indexOf(RESP_SUFFIX_MARKER, prefixMarkerPosition + RESP_PREFIX_MARKER_LENGTH))) {
            rawRespString = rawRespString.substring(prefixMarkerPosition + RESP_PREFIX_MARKER_LENGTH, suffixMarkerPosition);
        }
        return rawRespString;
    }

    /**
     * 提取有效的 json 内容,删除jsonr
     */
    public static String pruneRespJsonr(String rawRespString) {
        if (null == rawRespString) {
            return null;
        }
        int prefixMarkerPosition;
        int suffixMarkerPosition;
        if (null != rawRespString
                && -1 != (prefixMarkerPosition = rawRespString.indexOf(RESP_PREFIX_MARKER_WITH_JSONR))
                && -1 != (suffixMarkerPosition = rawRespString.indexOf(RESP_SUFFIX_MARKER_WITH_JSONR, prefixMarkerPosition + RESP_PREFIX_MARKER_WITH_JSONR_LENGTH))) {
            rawRespString = rawRespString.substring(prefixMarkerPosition + RESP_PREFIX_MARKER_WITH_JSONR_LENGTH, suffixMarkerPosition);
        }
        return rawRespString;
    }

    /**
     * 展开 url 参数
     */
    public static String inflateQueries(Map<String, String> params) {
        Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
        StringBuilder result = new StringBuilder();
        Map.Entry<String, String> entry = it.next();
        try {
            result.append(entry.getKey())
                    .append("=")
                    .append(URLEncoder.encode(entry.getValue(), HTTP_ENCODE));
            while (it.hasNext()) {
                entry = it.next();
                result.append("&")
                        .append(entry.getKey())
                        .append("=")
                        .append(URLEncoder.encode(entry.getValue(), HTTP_ENCODE));

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public static DataResponse<VoidData> parseVoidData(String parsed) {
        Gson gson = new Gson();
        return gson.fromJson(parsed, new TypeToken<DataResponse<VoidData>>() {
        }.getType());
    }
}
